{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pyaudio as pa\n",
    "\n",
    "p = pa.PyAudio()\n",
    "\n",
    "print('Available audio input devices:')\n",
    "for i in range(p.get_device_count()):\n",
    "    dev = p.get_device_info_by_index(i)\n",
    "    if dev.get('maxInputChannels'):\n",
    "        print(i, dev.get('name'))\n",
    "print('Please type input device ID:')\n",
    "dev_idx = int(input())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from frame_asr import FrameASR\n",
    "asr = FrameASR()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from bokeh.io import output_notebook, show, push_notebook\n",
    "from bokeh.plotting import figure\n",
    "import pyaudio as pa\n",
    "import time\n",
    "\n",
    "CHANNELS = 1\n",
    "RATE = 16000\n",
    "DURATION = 2.0\n",
    "CHUNK_SIZE = int(DURATION*RATE)\n",
    "\n",
    "output_notebook()\n",
    "\n",
    "fig = figure(plot_width=800, plot_height=400)\n",
    "signal = np.zeros(CHUNK_SIZE)\n",
    "source = {'x': np.arange(len(signal)), 'y': signal}\n",
    "plot = fig.line('x', 'y', source=source)\n",
    "show(fig, notebook_handle=True)\n",
    "\n",
    "\n",
    "def callback(in_data, frame_count, time_info, status):\n",
    "    signal = np.frombuffer(in_data, dtype=np.int16)\n",
    "    plot.data_source.data['y'] = signal\n",
    "    push_notebook()\n",
    "    pred = asr.transcribe(signal)\n",
    "    if len(pred.strip()):\n",
    "        print('\"{}\"'.format(pred))\n",
    "    return (in_data, pa.paContinue)\n",
    "\n",
    "\n",
    "\n",
    "stream = p.open(format=pa.paInt16,\n",
    "                channels=CHANNELS,\n",
    "                rate=RATE,\n",
    "                input=True,\n",
    "                input_device_index=dev_idx,\n",
    "                stream_callback=callback,\n",
    "                frames_per_buffer=CHUNK_SIZE)\n",
    "\n",
    "stream.start_stream()\n",
    "\n",
    "while stream.is_active():\n",
    "    time.sleep(0.1)\n",
    "\n",
    "stream.stop_stream()\n",
    "stream.close()\n",
    "\n",
    "p.terminate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
